package com.jd.org.service.impl;

import com.jd.basic.service.impl.BaseServiceImpl;
import com.jd.basic.util.MD5Utils;
import com.jd.basic.util.StrUtils;
import com.jd.org.domain.Employee;
import com.jd.org.mapper.EmployeeMapper;
import com.jd.org.service.IEmployeeService;
import com.jd.user.domain.Logininfo;
import com.jd.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
//@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {


//    @Autowired
//    private EmployeeMapper employeeMapper;
//
//
//    @Override
//    @Transactional
//    public void save(Employee employee) {
//        employeeMapper.save(employee);
//    }
//
//    @Override
//    @Transactional
//    public void remove(Long id) {
//        employeeMapper.remove(id);
//    }
//
//    @Override
//    @Transactional
//    public void update(Employee employee) {
//        employeeMapper.update(employee);
//    }
//
//    @Override
//    public Employee loadById(Long id) {
//        return employeeMapper.loadById(id);
//    }
//
//    @Override
//    public List<Employee> loadAll() {
//        return employeeMapper.loadAll();
//    }
//
//    @Override
//    public PageList<Employee> queryPage(EmployeeQuery employeeQuery) {
//        // 查询总条数
//        Integer totals = employeeMapper.queryPage(employeeQuery);
//        if (totals == 0){
//            return new PageList<>();
//        }
//
//        // 分页查询
//        List<Employee> data = employeeMapper.queryData(employeeQuery);
//
//        return new PageList<>(data,totals);
//    }
//
//    // 批量删除
//    @Override
//    public void patchRemove(List<Long> ids) {
//        employeeMapper.patchRemove(ids);
//    }
//
//    @Override
//    public List<Employee> deptTree() {
//        // 将所有的员工部门添加的Map中
//        List<Employee> employees = employeeMapper.loadAll();
//        Map<Long,Employee> map = new HashMap<>();
//        // 将每一个员工部门的id和对象保存在Map
//        for (Employee employee:employees){
//            map.put(employee.getId(),employee);
//        }
//
//        // 查询员工部门树
//        List<Employee> deptTree = new ArrayList<>();
//        for (Employee employee: employees){
//            // 如果没有父id - 顶级部门
//            if (employee.getDepartment_id()==null){
//                deptTree.add(employee);
//            }else {
//                // 获取父部门id
//                Long department_id = employee.getDepartment_id();
//                // 获取父部门
//                Employee departmentDept = map.get(department_id);
//                if(departmentDept != null){
//                    // 将自己添加到父部门的Children中去
//                    departmentDept.getChildren().add(employee);
//                }
//
//            }
//        }
//        return deptTree;
//    }

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private EmployeeMapper employeeMapper;



    @Override
    public void save(Employee employee) {
        // 处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);


        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(1);
        logininfo.setDisable(true);
        logininfoMapper.save(logininfo);

        employee.setLogininfo_id(logininfo.getId());
        super.save(employee);


        // employee - eid role_id
        if (employee.getRole_id() != null){
            employeeMapper.addEmployeeRole(employee);
        }



    }


    @Override
    public void remove(Long id) {
        // 删除登录信息
        Employee employee = employeeMapper.loadById(id);
        logininfoMapper.remove(employee.getLogininfo_id());


        // 通过empId删除中间表数据--注意外
        employeeMapper.delEmployeeRoleByEmpId(id);

        // 删除employee信息
        super.remove(id);

    }


    @Override
    public void update(Employee employee) {
        // 处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);
        super.update(employee);

        Long logininfoId = employeeMapper.loadById(employee.getId()).getLogininfo_id();

        // 创建logininfo
        Logininfo logininfo = logininfoMapper.loadById(logininfoId);
        BeanUtils.copyProperties(employee,logininfo);
        // 重置logininfoId
        logininfo.setId(logininfoId);
        logininfoMapper.update(logininfo);



        // 通过empId删除中间表数据 - 外键挂链要注意
        employeeMapper.delEmployeeRoleByEmpId(employee.getId());
        //再添加员工的角色信息到中间表：employee - eid roleId
        if (employee.getRole_id() != null){
            employeeMapper.addEmployeeRole(employee);
        }





    }

    @Override
    public List<Employee> findShopId(Long id) {
        return employeeMapper.findShopId(id);
    }



}
